const showBox = document.querySelector('.main > .cart > .cart_list > li > .IMG')
const rightBox = document.querySelector('.main > .cart > .cart_list > li > .right')
const cartListBox = document.querySelector('.main > .cart')
const cartList = JSON.parse(window.localStorage.getItem('cart')) || []
console.log(cartList)
const goodsId = window.sessionStorage.getItem('goodsId')
console.log(goodsId)
const item = {
  itemId: `${goodsId}`,
}
getList()
async function getList() {
  let info = {
    itemId: `${goodsId}`,
  }
  const res = await $.ajax({ url: '/detail', data: info, dataType: 'json' })
  console.log(res)

  bindBig(res.data.itemInfo.imgContent[0])
  bindRight(res.data)
}
function bindBig(list) {
  let str = ''
  str = `
<img src="${list}" alt="">
`
  showBox.innerHTML = str
}
function bindRight(list) {
  console.log(list)
  const str = `
<div class="title">${list.itemInfo.title}</div>
<div class="price" data-value="${list.itemInfo.priceSale}">￥${list.itemInfo.priceSale}</div>
<div class="number">
<button class="sub_btn" data-num="1">-</button>
<input type="text" value="1" class="inpt_vlue">
<button class="add_btn" data-num="1">+</button>
</div>
<div class="money">￥<input type="text" value="${list.itemInfo.priceSale}"></div>
<div>
<button class="delete_btn">删除</button>
</div>
`
  rightBox.innerHTML = str
}



cartListBox.addEventListener('click', async e => {
  if (e.target.className === 'delete_btn') {
    const DEL = document.querySelector('.DEL')
    DEL.style.display = 'none'
  }
  if (e.target.className === 'add_btn') {
    const input_vlue = document.querySelector('.inpt_vlue')
    input_vlue.value++
    const money = document.querySelector('.money > input')
    money.value = money.value * input_vlue.value
  }
  if (e.target.className === 'sub_btn') {
    const input_vlue = document.querySelector('.inpt_vlue')
    input_vlue.value--
    const money = document.querySelector('.money > input')
    money.value = money.value * input_vlue.value
    // 判断, 如果当前 num <= 1, 不能少了
    if (input_vlue.value <= 0) return alert('别再少了, 再少没有了')
    
  }
  if (e.target.className === 'select_item') {
    const goodsId = e.target.dataset.id
    const item = { itemId: `${goodsId}`, }
  }
  if (e.target.className === 'delAll') {
    const selectAll = document.querySelector('.select_all')
    if(!selectAll.checked) return
    const DEL = document.querySelector('.DEL')
    DEL.style.display = 'none'
  }
  if (e.target.className === 'clear') {
    const DEL = document.querySelector('.DEL')
    DEL.style.display = 'none'
  }
  if (e.target.className === 'pay') {
    alert('不卖给你!')
    const DEL = document.querySelector('.DEL')
    DEL.style.display = 'none'
  }
})